package com.xdf.zipperdye.service;

import com.xdf.zipperdye.model.BillCp;
import com.xdf.zipperdye.model.CpDetail;
import com.xdf.zipperdye.model.CpMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class BillCpServiceImpl implements BillCpService {
    @Autowired
    private CpMainService mainService;
    @Autowired
    private CpDetailService detailService;

    @Transactional
    @Override
    public BillCp insert(BillCp record) {
        CpMain main = record.getMain();
        main.setIid(0);
        main = mainService.insertSelective(main);
        List<CpDetail> details = record.getDetails();

        Integer mainId = main.getIid();
        details.forEach( item -> {
            item.setIid(0);
            item.setParent_id(mainId);
        });

        int ret = detailService.multiInsert(details);

        // 重新从DB获取最新数据
        List<CpDetail> newDetails = detailService.selectByParentId(mainId);
        CpMain newMain = mainService.selectByPrimaryKey(mainId);
        record.setMain(newMain);
        record.setDetails(newDetails);
        return record;
    }

    @Transactional
    @Override
    public BillCp update(BillCp record) {
        if( check4Update(record) == false )
            throw new RuntimeException("没有主键无法修改！");

        CpMain main = record.getMain();
        Integer id = main.getIid();
        List<CpDetail> details = record.getDetails();
        Integer ret1 = mainService.updateByPrimaryKeySelective(main);
        Integer ret2 = detailService.multiUpdate(details);

        main = mainService.selectByPrimaryKey(id);
        details = detailService.selectByParentId(id);
        record.setMain(main);
        record.setDetails(details);
        return record;
    }

    @Transactional
    @Override
    public int deleteByPrimaryKey(Integer iid) {
        if( iid <=0 ) throw new RuntimeException("主键不存在，无法删除！");

        detailService.deleteByParentId(iid);
        mainService.deleteByPrimaryKey(iid);
        return 1;
    }

    @Override
    public BillCp selectByPrimaryKey(Integer iid) {
        CpMain main = mainService.selectByPrimaryKey(iid);
        List<CpDetail> details = detailService.selectByParentId(iid);
        BillCp bill = new BillCp();
        bill.setMain(main);
        bill.setDetails(details);
        return bill;
    }

    @Override
    public BillCp selectByBillCode(String billCode) {
        if( billCode == null || billCode.length()<=0 ) throw new RuntimeException("单据编号不存在！");
        CpMain main = mainService.selectByBillcode(billCode);
        if( main == null ) throw new RuntimeException("单据编号不存在！");
        Integer id = main.getIid();

        List<CpDetail> details = detailService.selectByParentId(id);

        BillCp bill = new BillCp();
        bill.setMain(main);
        bill.setDetails(details);
        return bill;
    }

    // 返回 false 表示不可修改，相反 true 表示可以修改
    @Override
    public Boolean check4Update(BillCp record) {
        if( record == null ) return false;
        CpMain main = Optional.ofNullable(record.getMain()).orElse(new CpMain());
        Integer iid = Optional.ofNullable(main.getIid()).orElse(0);
        if( iid <=0 ) return false;
        return true;
    }
}
